package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.HouseDto;
import com.itheima.entity.House;
import com.itheima.entity.HouseOfRent;
import com.itheima.entity.User;
import com.itheima.service.HouseOfRentService;
import com.itheima.service.HouseService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @date 2023/4/23
 */
@RestController
@Slf4j
@RequestMapping("/house")
public class HouseController {
    @Autowired
    HouseService houseService;
    @Autowired
    HouseOfRentService houseOfRentService;
    @Autowired
    UserService userService;


    @PostMapping
    public R add(@RequestBody House house) {
        boolean save = houseService.save(house);
        if (save) {
            return R.success("保存成功");
        }
        return R.error("保存失败");
    }

    @DeleteMapping("/{id}")
    public R deleted(@PathVariable("id") Integer id) {
        House house = houseService.getById(id);
        if (house.getStatus() == 1) {
            return R.error("出租中无法删除");
        }
        boolean b = houseService.removeById(id);
        if (b) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Transactional
    @PutMapping
    public R update(@RequestBody HouseDto houseDto) {
        Integer status = houseDto.getStatus();
        if (status == 1) {
            House house = houseService.getById(houseDto.getUserId());
            house.setStatus(1);
            houseService.updateById(house);
            HouseOfRent houseOfRent = new HouseOfRent();
            houseOfRent.setHouseId(house.getId());
            houseOfRent.setUserId(houseDto.getUserId());
            houseOfRentService.save(houseOfRent);
            return R.success("出租成功");
        }
        houseService.updateById(houseDto);
        LambdaQueryWrapper<HouseOfRent> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HouseOfRent::getUserId, houseDto.getUserId());
        lqw.eq(HouseOfRent::getHouseId, houseDto.getId());
        houseOfRentService.remove(lqw);
        return R.success("退房成功");
    }

    @GetMapping("/page")
    public R doPage(@RequestParam(defaultValue = "1") Integer page,
                    @RequestParam(defaultValue = "10") Integer pageSize,
                    String houseType,
                    Integer status) {
        Page<HouseDto> houseDtoPage = new Page<>();
        Page<House> pageHouse = new Page<>(page, pageSize);
        LambdaQueryWrapper<House> lqw = new LambdaQueryWrapper<>();
        lqw.like(houseType != null, House::getHouseType, houseType);
        lqw.eq(status != null, House::getStatus, status);
        Page<House> page1 = houseService.page(pageHouse, lqw);
        BeanUtils.copyProperties(page1, houseDtoPage, "records");
        List<House> records = page1.getRecords();
        List<HouseDto> collect = records.stream().map((item) -> {
            Integer status1 = item.getStatus();
            HouseDto houseDto = new HouseDto();
            BeanUtils.copyProperties(item, houseDto);
            if (status1 == 0) {
                houseDto.setName(null);
                return houseDto;
            }
            Long houseId = item.getId();
            LambdaQueryWrapper<HouseOfRent> lqwR = new LambdaQueryWrapper<>();
            lqwR.eq(HouseOfRent::getHouseId, houseId);
            List<HouseOfRent> list = houseOfRentService.list(lqwR);
            if (list.size()==0){
                return houseDto;
            }
            HouseOfRent houseOfRent = list.get(0);
            Long userId = houseOfRent.getUserId();
            User user = userService.getById(userId);
            houseDto.setName(user.getName());
            houseDto.setUserId(userId);
            return houseDto;
        }).collect(Collectors.toList());
        return R.success(collect);
    }
}
